<?php

namespace common\app;

use cm\app\Sms;
use yii\helpers\Url;
use Yii;
include_once __DIR__ . '/user.php';
include_once __DIR__ . '/am.php';
include_once __DIR__ . '/log.php';
include_once __DIR__ . '/sms.php';

class App
{
    /**
     * @var User
     */
    public $user;

    /**
     * @var Am
     */
    public $am;

    /**
     * @var Log
     */
    public $log;

    /**
     * @var Sms
     */
    public $sms;

    function __construct()
    {
        $this->user = new User();
        $this->am = new Am();
        $this->log = new Log();
        $this->sms=new Sms();
    }


    public function reflectionAuth()
    {
        include_once __DIR__ . '/reflectionauth.php';
        return new ReflectionAuth();
    }

    public function url($route)
    {
        return Url::to($route);
    }

    public function apiUrl($route)
    {
        $data['appid'] = API_ID;
        $data['appkey'] = API_KEY;
        $data['time'] = time();
        ksort($data);

        $str = "";
        foreach ($data as $k => $v) {
            if (empty($str))
                $str .= "{$k}={$v}";
            else
                $str .= "&{$k}={$v}";
        }
        $sign = sha1($str);

        $url = (isset(Yii::$app->params['domain']) ? Yii::$app->params['domain'] : BASE_URL) . Url::to('/api/'.$route) . "?appid={$data['appid']}&sign={$sign}&time={$data['time']}";
        return $url;
    }

    public function urlTo($route)
    {
        $url = (isset(Yii::$app->params['domain']) ? 'wechat/' : BASE_URL) . Url::to('/api/'.$route);
        return $url;
    }

    function randStr($len = 32)
    {
        $str = '';
        for ($i = 0; $i < $len; $i++) {
            $str .= chr(mt_rand(33, 126));
        }
        return $str;
    }
}

